<?php
require_once('../phplib/include.inc.php');

class DoBackUp{
	public function DoIt(){
		$action = $_REQUEST['action'];
		if($action == 'del') $this->Del();
		elseif ($action == 'restore') $this->Restore();
		elseif ($action == 'backup') $this->Backup();
		else PageBase::Error('参数错误.');
		PageBase::Go('../home/backup.php');
	}
	public function Backup(){
		global $CONF;
		$host = $CONF['sql_host'];
		$user = $CONF['sql_user'];
		$pwd = $CONF['sql_pwd'];
		$db = $CONF['sql_db'];
		$port = $CONF['sql_port'];
		$path = $CONF['backup_path'];
		$command = "mysqldump -h $host:$port -u $user -p$pwd $db > $path/$db.".date("Y-m-d_H_i_s").".sql";
		if($host == '127.0.0.1' || $host == 'localhost')
			$command = "mysqldump -u $user -p$pwd $db > $path/$db.".date("Y-m-d_H_i_s").".".rand(10000,99999).".sql";
		exec($command);
		Util::AddLog('备份数据库');
	}
	public function Del(){
		global $CONF;
		if(!$CONF['allow_del_backup']) PageBase::Error('不允许删除数据库备份文件，请查看配置文件.');
		$file = $_REQUEST['file'];
		$file = $CONF['backup_path']."/".$file;
		@unlink($file);
		Util::AddLog('删除数据库备份文件:'.$file);
	}
	public function Restore(){		
		$this->Backup();
		global $CONF;
		$host = $CONF['sql_host'];
		$user = $CONF['sql_user'];
		$pwd = $CONF['sql_pwd'];
		$db = $CONF['sql_db'];
		$port = $CONF['sql_port'];
		$path = $CONF['backup_path'];
		
		$file = $_REQUEST['file'];
		$file = $path."/".$file;
		
		$command = "mysql $db < $file -h $host:$port -u $user -p$pwd";
		if($host == '127.0.0.1' || $host == 'localhost')
			$command = "mysql $db < $file -u $user -p$pwd";
			
		exec($command);
		Util::AddLog('恢复数据库，文件:'.$file);
	}
}
$dobackup = new DoBackUp();
$dobackup->DoIt();
?>